package service

import (
	"context"

	pb "helloworld/api/user/service/v1"
	"helloworld/app/user/service/internal/biz"
)

type UserService struct {
	pb.UnimplementedUserServer
	uc *biz.UserUsecase
}

func NewUserService(uc *biz.UserUsecase) *UserService {
	return &UserService{uc: uc}
}

func (s *UserService) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
	g, err := s.uc.CreateUser(ctx, &biz.User{Hello: req.Name})
	if err != nil {
		return nil, err
	}

	return &pb.HelloReply{Message: g.Hello + " world"}, nil
}
func (s *UserService) StartMq(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
	_, err := s.uc.Send(ctx)
	if err != nil {
		return nil, err
	}

	return &pb.HelloReply{Message: "发送成功"}, nil
}

func (s *UserService) Consume(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
	_, err := s.uc.Receive(ctx)
	if err != nil {
		return nil, err
	}

	return &pb.HelloReply{Message: "接收成功"}, nil
}
